package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class RegisterPage extends Utils {
    public static String url = "http://49.235.61.184:8080/user/register";

    public RegisterPage() {
        super(url);
    }
    /**
     * 注册页面可以正常打开
     */
    public void checkRegisterPageRight(){
        //检查输入框
        driver.findElement(By.cssSelector("#name"));
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#rePassword"));
        //检查按钮
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button"));
    }
    /**
     * 测试一：成功注册-输入正确的姓名、登录账号、密码、密码确认
     */
    public void registerSuc() throws IOException {
        /*//先清空输入框的内容
        driver.findElement(By.cssSelector("#name")).clear();
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#rePassword")).clear();*/
        //刷新
        driver.navigate().refresh();

        //输入正确的姓名、登录账号、密码、密码确认
        driver.findElement(By.cssSelector("#name")).sendKeys("linlin");
        driver.findElement(By.cssSelector("#username")).sendKeys("2w");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        //检查是否注册成功，跳转到登录页面
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.top___1C1Zi > div.header___5xZ3f > span"));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    /**
     * 测试二：注册失败-姓名为空，其他输入正确
     */
    public void registerFail1() throws IOException, InterruptedException {
        //先清空输入框的内容
        driver.findElement(By.cssSelector("#name")).clear();
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#rePassword")).clear();

        //姓名为空，其他输入正确
        driver.findElement(By.cssSelector("#username")).sendKeys("linlin");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();
        //检查是否注册失败
        Thread.sleep(1000);
        String tip = driver.findElement(By.cssSelector("#name_help > div")).getText();
        /*WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement tipElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#name_help > div")));
        String tip = tipElement.getText();*/
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        assert tip.equals("用户名是必填项！");
    }

    /**
     * 测试三：输入已存在的账号
     */
    public void registerFail2() throws IOException {
        /*//先清空输入框的内容
        driver.findElement(By.cssSelector("#name")).clear();
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#rePassword")).clear();*/
        driver.navigate().refresh();

        //姓名为空，其他输入正确
        driver.findElement(By.cssSelector("#name")).sendKeys("xiaolin");
        driver.findElement(By.cssSelector("#username")).sendKeys("615");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();

        //检查是否注册失败
        String tip = driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert tip.equals("账号已存在");
    }
    /**
     * 测试三：输入的密码和密码确认不一样
     */
    public void registerFail3() throws IOException {
        driver.navigate().refresh();

        driver.findElement(By.cssSelector("#name")).sendKeys("xiaolin");
        driver.findElement(By.cssSelector("#username")).sendKeys("615");
        //密码不一致
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123654");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();


        //检查是否注册失败
        String tip = driver.findElement(By.cssSelector("#rePassword_help > div")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert tip.equals("两次密码不一致!");
    }
    /**
     * 测试三：密码小于6位
     */
    public void registerFail4() throws IOException {
        driver.navigate().refresh();

        driver.findElement(By.cssSelector("#name")).sendKeys("xiaolin");
        driver.findElement(By.cssSelector("#username")).sendKeys("615");
        //密码不一致
        driver.findElement(By.cssSelector("#password")).sendKeys("12345");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("12345");
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();


        //检查是否注册失败
        String tip = driver.findElement(By.cssSelector("#password_help > div")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert tip.equals("密码至少6位");
    }
}
